package cn.itcast.mq.reception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 消息接收
 *
 * @Author mr.wu
 * @Date 2025/1/5 11:14
 */
@Component
@Slf4j
public class SpringRabbitListener {

    /**
     * 监听消息
     *
     * @param message
     */

    @RabbitListener(queues = "simple.queue")
    public void receiveMessage(String message) {
        log.info("SpringRabbitListener接收到消息：{}", message);
    }

    /**
     * 工作队列模型-监听队列1
     *
     * @param message
     */
    @RabbitListener(queues = "Work.queue")
    public void receiveMessage1(String message) throws InterruptedException {
        log.info("工作队列模型1接收到的数据：" + message);
        TimeUnit.MILLISECONDS.sleep(10);

    }

    /**
     * 工作队列模型-监听队列2
     *
     * @param message
     */
    @RabbitListener(queues = "Work.queue")
    public void receiveMessage2(String message) throws InterruptedException {
        log.info("SpringRabbitListener接收到消息：{}", message);
        TimeUnit.MILLISECONDS.sleep(100);
    }

    /**
     * 广播模型-监听队列1
     *
     */
    @RabbitListener(queues = "fanout.queue1")
    public void fandoutListenter1(String message) {
        log.info("fanout.queue1接收到消息：{}", message);
    }

    /**
     * 广播模型-监听队列2
     *
     */
    @RabbitListener(queues = "fanout.queue2")
    public void fandoutListenter2(String message) {
        log.info("fanout.queue2接收到消息：{}", message);
    }

    /**
     * 路由模型-监听队列1
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "direct.queue1"),
            exchange = @Exchange("direct.exchange"),key = {"blue","red"}))
    public void directListenter1(String message) {
        log.info("direct.queue1接收到消息：{}", message);
    }

    /**
     * 路由模型-监听队列2
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "direct.queue2"),
            exchange = @Exchange(value = "direct.exchange",type = ExchangeTypes.DIRECT),key = {"yellow","red"}))
    public void directListenter2(String message) {
        log.info("direct.queue2接收到消息：{}", message);
    }

    /**
     * Topic 主题模型-监听队列1
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "topic.queue1"),
            exchange = @Exchange(value = "topic.exchange",type = ExchangeTypes.TOPIC),key = "china.#"))
    public void topicListenter1(String message) {
        log.info("topic.queue1接收到消息：{}", message);
    }

    /**
     * Topic 主题模型-监听队列2
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = "topic.queue2"),
            exchange = @Exchange(value = "topic.exchange",type = ExchangeTypes.TOPIC),key = "*.news"))
    public void topicListenter2(String message) {
        log.info("topic.queue2接收到消息：{}", message);
    }

    /**
     * 消息转换器-消费者接收
     */
    @RabbitListener(queues = "object.queue")
    public void objectListenter(Map<String, Object> msg) {
        log.info("object.queue接收到消息：{}",msg);
    }
}
